using System;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.Model.ColourSpecifications
{
    internal class SuccessRateMetric : MetricBase
    {
        public static readonly IMetric Instance = new SuccessRateMetric();

        private SuccessRateMetric()
        {
        }

        public override string NameSingular
        {
            get { return "success-rate"; }
        }

        public override string NamePlural
        {
            get { return "success-rate"; }
        }

        public override Func<IMonitorState, decimal?> GetValueFunction
        {
            get { return i => i.SuccessRate == null ? (decimal?) null : i.SuccessRate.Value; }
        }

        public override bool IsValidValue(decimal value, out string message)
        {
            if(value < 0 || value > 1)
            {
                message = string.Format("Must be between 0% and 100% (0.00 and 1.00)");
                return false;
            }

            if (value > MonitorState.MaximumStoredFailureCount)
            {
                message = string.Format("Cannot be more than {0}. This is the number of errors stored", MonitorState.MaximumStoredFailureCount);
                return false;
            }
            message = null;
            return true;
        }
    }
}